<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Configuration
 *
 * @author MinhNguyen
 */

class User {
    //put your code here
    public $id;
    public $name;
    public $password;
    public $email;
    public $phone;
    public $address;
    public $company;
    public $position;
    public $status;
    public $dateregistered;
    public $token;


    public function __get($name) {
        return $this->$name;
    }
    
    public function __set($name, $value) {
        $this->$name = $value;
    }   

    /**
     * It's constuctor of User with email
     */
    
    public function __construct() {
        //allocate your stuff
    }

    /**
     * Print message if email is not found in database
     * or print error message if can not connect database
     */
    public static function withEmail($email) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM Users WHERE email = '$email'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->name = $rows["name"];
                $instance->password = $rows["password"];
                $instance->email = $rows["email"];
                $instance->phone = $rows["phone"];
                $instance->address = $rows["address"];
                $instance->company = $rows["company"];
                $instance->position = $rows["position"];
                $instance->status = $rows["status"];
                $instance->dateregistered = $rows["dateregistered"];
                $instance->token = $rows["token"];
                return $instance;
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "404", "Not found any user with provided email");
                $controller->returnError();
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }
    
    public static function withData($nameUser, $password, $email) {
        $instance = new self();
        $instance->nameUser=$nameUser;
        $instance->password=$password;
        $instance->email=$email;
        return $instance;
    }
}
?>
